package com.example.sqliteapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/*
findViewById方法找控件的时候，是在setContentView(R.layout.activity_main2);指定的布局文件中找控件
 */
public class Main2Activity extends AppCompatActivity {

    private String[] names;
    private String[] phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //1-module 数据
        String s1="何斌,冯帅,马亚文,张凌霄,孙玉海,杨景岚,王昱璇,付佳杰,张宏伟,王小龙,周湘洋,冯敏,胡慧婷,高敏,杨晓乐,刘煜彤,梁泽邦,高航,杜治国,田旭,黄勇彪,李安祥,杨虎生,韩利伟,陈昱桥,王丹,石茹玉,白雪,梁姣姣,郭磊磊,贺宇浩,林玉翩,曾维义,齐迪帆,郭树育,黄钰奇,安卓奥博,谢帅强,侯泽宇";
        String s2="1666129302,1867111102,1867111103,1867111104,1867111106,1867111110,1867111112,1867111115,1867111125,1867111127,1867111129,1867111134,1867111141,1867111145,1867111146,1867111147,1867112103,1867111202,1867111205,1867111206,1867111209,1867111210,1867111212,1867111222,1867111239,1867111244,1867111245,1867111246,1867111247,1867111248,1867111249,1874135222,1874146128,1874146307,1861102206,1861158208,1867111121,1867111226,1867111243";
        //splite切分子串，多个字符串放到一个数组
        names = s1.split(",");
        phone = s2.split(",");
        //2-view视图 ListView
        ListView lvInfo = (ListView) findViewById(R.id.lv_info);
        //3-Control 给ListView加Adapter适配器
        //适配器的参数需要一个ListAdapter接口对象，由于ListAdapter接口抽象方法多
        //使用ListAdapter接口的子类BaseAdapter
        lvInfo.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                //总条目数，要展示的数据的总数
                return phone.length;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View convertView, ViewGroup viewGroup) {
                ViewHolder viewHolder=new ViewHolder();

                //每个条目的view视图
                /*新建一个文本框控件，设置属性，返回
                TextView tvName=new TextView(getApplicationContext());
                tvName.setText(names[i]+phone[i]);
                return tvName;
                 */
                //判断convertView是否为null，如果为null，创建view；非空，直接使用（复用）
                if (convertView==null){
                    //inflate方法就是根据布局文件的id找到布局文件转换成View控件
                    convertView=View.inflate(getApplicationContext(),R.layout.list_item,null);
                    //找到view中的控件，放到ViewHolder中
                    viewHolder.tvName=(TextView) convertView.findViewById(R.id.list_item_name);
                   viewHolder.tvPhone= (TextView) convertView.findViewById(R.id.list_item_phone);
                    convertView.setTag(viewHolder);
                }else {
                    //取出convertView中的ViewHolder
                    viewHolder= (ViewHolder) convertView.getTag();
                }


                //
                //找到文本框控件
//                TextView tvName= (TextView) convertView.findViewById(R.id.list_item_name);
//                TextView tvPhone= (TextView) convertView.findViewById(R.id.list_item_phone);
                viewHolder.tvName.setText(names[i]);
                viewHolder.tvPhone.setText(phone[i]);
                //3-返回数据项要显示的view1
                return convertView;



                /* 将数据要显示的view放到一个布局文件中

                //1-找到数据项要显示的那个布局文件list_item.xml,转换成view1
                //inflate方法就是根据布局文件的id找到布局文件转换成View控件
                View view1=View.inflate(getApplicationContext(),R.layout.list_item,null);
                //2-1-找到布局文件即view1中的文本框控件
                TextView tvName= (TextView) view1.findViewById(R.id.list_item_name);
                TextView tvPhone= (TextView) view1.findViewById(R.id.list_item_phone);
                //2-2设置文本框
                tvName.setText(names[i]);
                tvPhone.setText(phone[i]);
                //3-返回数据项要显示的view1
                return  view1;
                */
            }
        });
    }
    class ViewHolder{
        TextView tvName;
        TextView tvPhone;
    }
}
